package zsc.ruanc.practicaltraining.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import zsc.ruanc.practicaltraining.model.result.JsonResult;
import zsc.ruanc.practicaltraining.model.vo.AdminEmailVo;
import zsc.ruanc.practicaltraining.model.vo.EmailVo;
import zsc.ruanc.practicaltraining.model.vo.emailVo;
import zsc.ruanc.practicaltraining.model.vo.findPasswordVo;
import zsc.ruanc.practicaltraining.service.EmailService;
import zsc.ruanc.practicaltraining.utils.DataVerificationUtils;
import zsc.ruanc.practicaltraining.utils.PrintfUtils;
import zsc.ruanc.practicaltraining.utils.ResultUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 邮件控制器
 */
@RestController
@RequestMapping("/email")
@Slf4j
@Api(tags = "邮件服务模块")
public class EmailController {

    @Autowired
    private EmailService emailService;



    /**
     *  发送注册验证码
     * @param emailVo
     * @return
     */
    @ApiOperation(value = "申请账号时发验证码", httpMethod = "POST")
    @PostMapping("/sendCode")
    public JsonResult sendCode(@Validated @RequestBody EmailVo emailVo) {

        PrintfUtils.print(emailVo); // 打印请求信息

        Map map = new HashMap();
        map.put("code", emailService.sendCode(emailVo));
        return ResultUtils.success(map, "已收到请求");
    }

    /**
     * 找回密码
     * 1.先发邮件
     * @param
     * @return
     */
    @PostMapping("/sendFindPasswordCode")
    @ApiOperation(value = "找回密码发验证码", httpMethod = "POST")
    public JsonResult sendFindPasswordCode(@Validated @RequestBody emailVo emailVo) {

        PrintfUtils.print(emailVo); // 打印请求信息

        Map map = new HashMap();
        map.put("code", emailService.findPassword(emailVo.getEmail()));

        return ResultUtils.success(map, "已收到请求");
    }

    @PostMapping("/updateEmailCode")
    @ApiOperation(value = "更改邮箱时发验证码", httpMethod = "POST")
    public JsonResult updateEmailCode(@Validated @RequestBody emailVo emailVo) {

        PrintfUtils.print(emailVo); // 打印请求信息

        Map map = new HashMap();
        map.put("code", emailService.updateEmail(emailVo.getEmail()));

        return ResultUtils.success(map, "已收到请求");
    }

    /**
     * 管理员给用户发信息
     * @param vo
     * @return
     */
    @ApiOperation(value = "管理员给用户发信息", httpMethod = "POST")
    @PostMapping("/adminSendEmail")
    public JsonResult adminSendEmail(@Validated @RequestBody AdminEmailVo vo) {
        emailService.adminSend(vo.getId(), vo.getText());
        return ResultUtils.success("请求成功！！！");
    }



}
